//
//  HomeViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/8.
//

import Foundation
import SnapKit
import TSVideoKit

class HomeViewController: BasicBGViewController {
    lazy var topView: TopBarView = TopBarView(imgName: "ic_music", types: [.addButton])
    lazy var likeItem: HomeTopGroupItemView = HomeTopGroupItemView()
    lazy var recentlyItem: HomeTopGroupRecentlyItemView = HomeTopGroupRecentlyItemView()
    lazy var mostItem: HomeTopGroupMostItemView = HomeTopGroupMostItemView()

    lazy var titleGroup: UILabel = .simpleLabel(text: "My Music", font: .systemFont18, color: .white)
    
    
    lazy var listVc: SongListViewController = {
        let vModel = SongListViewModel()
        let vc = SongListViewController(viewModel: vModel)
        return vc
    }()

    weak var importMenuView: BubbleMenuView?

    override func viewDidLoad() {
        super.viewDidLoad()
        updateInfoData()
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(topView)
        view.addSubview(likeItem)
        view.addSubview(recentlyItem)
        view.addSubview(mostItem)
        view.addSubview(titleGroup)
        view.addSubview(listVc.view)
        addChild(listVc)
        listVc.didMove(toParent: self)
        addTargets()
    }

    func addTargets() {
        topView.addButton.addTarget(self, action: #selector(showImportMenu), for: .touchUpInside)
        likeItem.addTarget(self, action: #selector(likeItemAction), for: .touchUpInside)
        recentlyItem.addTarget(self, action: #selector(recentlyAction), for: .touchUpInside)
        mostItem.addTarget(self, action: #selector(mostPlayedAction), for: .touchUpInside)
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateInfoData), name: .K_ImportSuccessNotifaction, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateInfoData), name: kDataChangedNotifactionName, object: nil)
    }

    @objc func updateInfoData() {
        let favouriteCount = TSVideoOperator.shared.dataManager.fetchFavouriteVideos(sort: NSSortDescriptor(key: "createdAt", ascending: true)).count
        let recentlyCount = TSVideoOperator.shared.dataManager.fetchAllVideos().count
        let mostPlayedCount = TSVideoOperator.shared.dataManager.fetchAllVideos().count

        likeItem.titleLabel.text = "\(favouriteCount)"
        recentlyItem.titleLabel.text = "\(recentlyCount)"
        mostItem.titleLabel.text = "\(mostPlayedCount)"
    }
    

    @objc func likeItemAction() {
        let vc = ListDetailViewController(listType: .favourite)
        navigationController?.pushViewController(vc, animated: true)
    }

    @objc func recentlyAction() {
        let vc = ListDetailViewController(listType: .recently)
        navigationController?.pushViewController(vc, animated: true)
    }

    @objc func mostPlayedAction() {
        let vc = ListDetailViewController(listType: .mostPlayed)
        navigationController?.pushViewController(vc, animated: true)
    }

    override func makeConstraints() {
        super.makeConstraints()
        topView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(44)
        }

        likeItem.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.width.equalTo(162)
            make.height.equalTo(170)
            make.top.equalTo(topView.snp.bottom).offset(16)
        }

        recentlyItem.snp.makeConstraints { make in
            make.leading.equalTo(likeItem.snp.trailing).offset(12)
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalTo(likeItem)
            make.height.equalTo(80)
        }

        mostItem.snp.makeConstraints { make in
            make.leading.equalTo(likeItem.snp.trailing).offset(12)
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalTo(recentlyItem.snp.bottom).offset(10)
            make.height.equalTo(80)
        }

        titleGroup.snp.makeConstraints { make in
            make.top.equalTo(likeItem.snp.bottom).offset(30)
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
        }

        listVc.view.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-tabbarHeight)
            make.top.equalTo(titleGroup.snp.bottom).offset(16)
        }
    }
}

extension HomeViewController {
    @objc func showImportMenu() {
        let menuView = BubbleMenuView()
        menuView.position = FitManager.isAr ? .topLeft : .topRight
        menuView.actionHanlder = { [weak self] type in
            self?.menuClick(type: type)
        }
        view.addSubview(menuView)
        menuView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalTo(self.topView.snp.bottom).offset(0)
        }
        importMenuView = menuView
    }

    func menuClick(type: ImportSource) {
        importMenuView?.removeFromSuperview()
        if type == .library {
            ImportFilesManager.shared.openPhotoLibrary(parent: self)
        } else if type == .file {
            ImportFilesManager.shared.openFileDocument(parent: self, completion: nil)
        }
    }
}
